package com.tianma315.core.trace.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.exception.MessageException;
import com.tianma315.core.constant.StateEnum;

import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.service.ProductService;
import com.tianma315.core.trace.dao.TraceabilityFileDao;
import com.tianma315.core.trace.domain.TraceabilityFileDO;
import com.tianma315.core.trace.service.TraceabilityFileService;
import com.tianma315.core.trace.vo.TraceOutVO;
import com.tianma315.core.trace.vo.TraceabilityFileVO;
import com.tianma315.core.tymenu.domain.TyMenuVO;
import com.tianma315.core.tymenu.service.TyMenuService;


import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 
 * <pre>
 * 溯源档案
 * </pre>
 * <small> 2020-02-21 09:31:41 | Aron</small>
 */
@Service
public class TraceabilityFileServiceImpl extends CoreServiceImpl<TraceabilityFileDao, TraceabilityFileDO> implements TraceabilityFileService {


    @Autowired
    private ProductService productService;

    @Autowired
    private TyMenuService tyMenuService;
    @Autowired
    TraceabilityFileDao traceabilityFileDao;

    @Override
    public List<TraceabilityFileDO> getTraceabilityFileDOByTraceOutId(Integer traceOutId) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("trace_out_id",traceOutId);
        wrapper.eq("state", StateEnum.not_del);
        List<TraceabilityFileDO> traceabilityFileDOS = selectList(wrapper);
        return traceabilityFileDOS;
    }



    @Override
    public Page<TraceabilityFileVO> getTraceabilityFilePage(Integer pageNumber, Integer pageSize, TraceabilityFileDO traceabilityFileDTO) {
        Page page = new Page(pageNumber,pageSize);
        List<TraceOutVO> list = traceabilityFileDao.getTraceabilityFileVO(page,traceabilityFileDTO);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<TraceabilityFileDO> getTraceabilityFileDOList() {
        EntityWrapper<TraceabilityFileDO> wrapper = new EntityWrapper<>();
        wrapper.eq("state",0);
        return selectList(wrapper);
    }

    @Override
    public boolean updateStateById(Integer traceabilityFileId) {
        TraceabilityFileDO traceabilityFileDO = new TraceabilityFileDO();
        traceabilityFileDO.setState(StateEnum.is_del.getCode());
        traceabilityFileDO.setTraceabilityFileId(traceabilityFileId);
        Boolean result = updateById(traceabilityFileDO);
        return result;
    }

    @Override
    public JSONObject getDataByTraceabilityFileId(Integer id) {

        JSONObject result = new JSONObject();
        //获取溯源档案
        TraceabilityFileDO traceabilityFileDO = selectById(id);
        Optional.ofNullable(traceabilityFileDO).orElseThrow(()->new MessageException("没有找到溯源档案"));
        /*if (traceabilityFileDO == null){
            throw new MessageException("没有找到溯源档案");
        }*/

        //获取产品信息
        ProductDO productDO = productService.getProductById(traceabilityFileDO.getProductId().intValue());
        Optional.ofNullable(productDO).orElseThrow(()->new MessageException("没有找到产品"));
      /*  if (productDO == null){
            throw new MessageException("没有找到产品");
        }*/


        //根据档案id查找整个档案流程
        List<TyMenuVO> tyMenuVOS = tyMenuService.getTyMenuByTraceabilityFileId(traceabilityFileDO.getTraceabilityFileId());
        Optional.ofNullable(tyMenuVOS).orElseThrow(()->new MessageException("没找到档案流程"));
       /* if (tyMenuVOS.isEmpty()){
            throw new MessageException("没找到档案流程");
        }*/


        result.put("code",200);
        result.put("product",productDO);
        result.put("produceCenter",tyMenuVOS);
//        result.put("tyMenuSonList",(List<TyMenuVO>)tyMenuVOS.get("tyMenuSonList"));

        return result;
    }

}
